#include "form2.h"
#include "ui_form2.h"


Form2::Form2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form2)
{
    ui->setupUi(this);
    QList<QCameraInfo> infos = QCameraInfo::availableCameras();
    QCameraInfo info = infos.at(0);
    Cname = info.deviceName();
}

Form2::~Form2()
{
    delete ui;
}

//打开相机
void Form2::on_pushButton_clicked()
{
    ca = new QCamera(Cname.toUtf8(),this);

    ui->label->show();

    //实例化截屏对象
    cap=new QCameraImageCapture(ca);
    //连接截屏信号和显示截屏的函数
    connect(cap,SIGNAL(imageCaptured(int,QImage)),this,SLOT(show_pic(int,QImage)));

    QCameraViewfinder *v = new QCameraViewfinder(ui->label);
    v->resize(ui->label->size());
    ca->setViewfinder(v);
    v->show();
    ca->start();
}

//关闭相机
void Form2::on_pushButton_2_clicked()
{
    ca->stop();
    ui->label->hide();
    this->close();
  //  this->parentWidget();
}

//图像截取
void Form2::on_pushButton_3_clicked()
{
    //获取时间
   QTime t = QTime::currentTime();
   QString str="D:/Enterprise training/day_4/src/untitled/photo/"+t.toString("hh-mm-ss")+".jpg";
   qDebug()<<str;
   cap->capture(str);
}

//图片显示槽函数
void Form2::show_pic(int id,const QImage &pre)
{
    qDebug()<<id;
    //将QImage转化为Qpixmap
    QPixmap pix=QPixmap::fromImage(pre);
    //显示图片
    pix=pix.scaled(ui->label_2->size());
    ui->label_2->setPixmap(pix);
}
